package com.ssyc.gsk_teacher.activity;

import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.utils.MaxTextLengthFilter;
import com.ssyc.common.utils.RegularUtils;
import com.ssyc.common.utils.SnackbarUtil;
import com.ssyc.gsk_teacher.R;


/**
 * 通用的编辑学生相关信息的界面
 * Created by Administrator on 2018/2/26 0026.
 */

public class TeacherEditStudentActivity extends BaseActivity implements View.OnClickListener {

    //返回
    private ImageView ivBack;

    //标题
    private TextView tvTitle;

    //输出框
    private EditText etInput;

    //显示输入框的文字
    private TextView tvNum;

    //保存按钮
    private Button btSave;

    //传递过来的类型区别码
    private int type = -1;

    //通过Intent传值的ResultCode
    public static final int CHINESERNAME = 0X210;
    public static final int ENGLISEHNAME = 0X220;
    public static final int PHONE = 0X230;
    public static final int SCHOOL = 0X240;
    public static final int GRADE = 0X250;
    public static final int LOVE = 0X260;
    public static final int STATE = 0X270;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return "";
    }

    @Override
    protected int getContentLayout() {
        return R.layout.teacher_activity_edit_student;
    }

    @Override
    protected void init() {

        showContent();

        //初始化视图
        initView();

        //设置监听
        initListener();

        //初始状态
        initState();
    }

    /**
     * 初始化View
     */
    private void initView() {
        etInput = findViewById(R.id.et_edit);
        tvBaseRight.setOnClickListener(this);
        tvTitle = findViewById(R.id.tv_title);
        btSave = findViewById(R.id.bt_commit);
        btSave.setOnClickListener(this);
        tvNum = findViewById(R.id.tv_num);
        ivBack = findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
    }

    /**
     * 初始状态
     */
    private void initState() {
        type = getIntent().getIntExtra("type", -1);
        String title = getIntent().getStringExtra("title");
        tvTitle.setText(title);
        if (type == TeacherAddStudentActivity.PHONE) {
            etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(12)});
            etInput.setInputType(InputType.TYPE_CLASS_PHONE);
            tvNum.setText("0/11");
        } else if (type == TeacherAddStudentActivity.STATE) {
            etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(9)});
            etInput.setInputType(InputType.TYPE_CLASS_TEXT);
            tvNum.setText("0/8");
        } else if(type == TeacherAddStudentActivity.CHINESERNAME){
            //中文5字
            etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(6)});
            etInput.setInputType(InputType.TYPE_CLASS_TEXT);
            tvNum.setText("0/5");

        } else if(type == TeacherAddStudentActivity.SCHOOL){
            //学校25字
            etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(26)});
            etInput.setInputType(InputType.TYPE_CLASS_TEXT);
            tvNum.setText("0/25");
        }else if(type == TeacherAddStudentActivity.ENGLISEHNAME){
            //英文12字
            etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(13)});
            etInput.setInputType(InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
            etInput.setHint("请输入英文字母");
            tvNum.setText("0/12");
            String regular = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            etInput.setKeyListener(new DigitsKeyListener() {
                @Override
                public int getInputType() {
                    return InputType.TYPE_TEXT_VARIATION_PASSWORD;
                }

                @Override
                protected char[] getAcceptedChars() {
                    char[] ac = regular.toCharArray();
                    return ac;
                }
            });
        }else {
            etInput.setFilters(new InputFilter[]{new MaxTextLengthFilter(13)});
            etInput.setInputType(InputType.TYPE_CLASS_TEXT);
            tvNum.setText("0/12");
        }
    }

    /**
     * 设置输入监听
     */
    private void initListener() {

        etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String content = etInput.getText().toString().trim();
                if (type == TeacherAddStudentActivity.PHONE) {
                    //电话号码
                    if (!TextUtils.isEmpty(content)) {
                        if (content.length() > 10) {
                            tvNum.setText("11/11");
                        } else {
                            tvNum.setText(content.length() + "/11");
                        }
                    }else{
                        tvNum.setText("0/11");
                    }

                } else if (type == TeacherAddStudentActivity.STATE) {

                    if (!TextUtils.isEmpty(content)) {
                        if (content.length() > 7) {
                            tvNum.setText("8/8");
                        } else {
                            tvNum.setText(content.length() + "/8");
                        }
                    }else{
                        tvNum.setText("0/8");
                    }
                } else if(type == TeacherAddStudentActivity.CHINESERNAME){
                    //中文5字
                    if (!TextUtils.isEmpty(content)) {
                        if (content.length() > 4) {
                            tvNum.setText("5/5");
                        } else {
                            tvNum.setText(content.length() + "/5");
                        }
                    }else{
                        tvNum.setText("0/5");
                    }
                }else if(type == TeacherAddStudentActivity.SCHOOL){
                    //学校
                    if (!TextUtils.isEmpty(content)) {
                        if (content.length() > 24) {
                            tvNum.setText("25/25");
                        } else {
                            tvNum.setText(content.length() + "/25");
                        }
                    }else{
                        tvNum.setText("0/25");
                    }
                }
                else {
                    //其它
                    if (!TextUtils.isEmpty(content)) {
                        if (content.length() > 11) {
                            tvNum.setText("12/12");
                        } else {
                            tvNum.setText(content.length() + "/12");
                        }
                    }else{
                        tvNum.setText("0/12");
                    }
                }
            }
        });
    }


    /**
     * 页面控件点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_commit) {
            //保存
            doSaveAction();
        } else if (id == R.id.iv_back) {
            //返回
            finish();
        }
    }

    /**
     * 点击保存后的操作
     */
    private void doSaveAction() {
        String content = etInput.getText().toString().trim();
        Intent intent = new Intent();
        if (type == TeacherAddStudentActivity.CHINESERNAME) {
            //编辑中文名称
            setResultIntent(content, "请输入中文名称", intent, CHINESERNAME);
        } else if (type == TeacherAddStudentActivity.ENGLISEHNAME) {

//            if(!englishName(content)){
//                //正则只能输入英文和数字
//                SnackbarUtil.ShortSnackbar(llBaseMain, "只能输入英文字母", Snackbar.LENGTH_SHORT).show();
//                return;
//            }



            //编辑英文名称
            setResultIntent(content, "请输入英文名称", intent, ENGLISEHNAME);
        } else if (type == TeacherAddStudentActivity.PHONE) {
            //编辑电话
            if (TextUtils.isEmpty(content)) {
                SnackbarUtil.ShortSnackbar(llBaseMain, "请输入电话号码", Snackbar.LENGTH_SHORT).show();
                return;
            } else if (!RegularUtils.isPhoneNumber(content)) {
                SnackbarUtil.ShortSnackbar(llBaseMain, "输入的电话号码非法", Snackbar.LENGTH_SHORT).show();
                return;
            } else {
                intent.putExtra("data", content);
                setResult(PHONE, intent);
                finish();
            }
        } else if (type == TeacherAddStudentActivity.SCHOOL) {
            //编辑学校
            setResultIntent(content, "请输入学校号码", intent, SCHOOL);
        } else if (type == TeacherAddStudentActivity.GRADE) {
            //编辑年级
            setResultIntent(content, "请输入年级", intent, GRADE);
        } else if (type == TeacherAddStudentActivity.LOVE) {
            //编辑兴趣爱好
            setResultIntent(content, "请输入兴趣爱好", intent, LOVE);
        } else if (type == TeacherAddStudentActivity.STATE) {
            //编辑兴趣爱好
            setResultIntent(content, "请输入缴费状态", intent, STATE);
        }
    }

    private boolean englishName(String content) {
        // 只允许字母
        return content.matches("^[A-Za-z]$");
    }

    /**
     * 统一处理回传信息
     *
     * @param content
     * @param intent
     * @param resultCode
     */
    private void setResultIntent(String content, String message, Intent intent, int resultCode) {
        if (TextUtils.isEmpty(content)) {
            SnackbarUtil.ShortSnackbar(llBaseMain, message, Snackbar.LENGTH_SHORT).show();
            return;
        } else {
            intent.putExtra("data", content);
            setResult(resultCode, intent);
            finish();
        }
    }
}
